import 'dart:convert';

import 'package:app/model/user_model.dart';
import 'package:shared_preferences/shared_preferences.dart';

class SharedPreferencesUtil {

  static SharedPreferences preferences;

  static Future<bool> getInstance() async{
    preferences = await SharedPreferences.getInstance();
    return true;
  }

  static void clearUserData() {
    removeUser();
  }

  static void saveUser(UserModel user) {
    preferences.setString("consignor_user_info", jsonEncode(user.toJSON()));
  }

  static UserModel getUser() {
    String jsonStr = preferences.getString("consignor_user_info");
    if(jsonStr == null){
      return null;
    }
    UserModel user = UserModel.fromJson(json.decode(jsonStr));
    return user;
  }

  static void removeUser() {
    preferences.remove("user_info");
  }
}
